namespace SimpleWebServices.Web.UI.Html.Elements
{
    using System.Xml.Serialization;
    using System.Xml.XPath;
    using SimpleWebServices;
    using Xunit;

    public sealed class TextAreaFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<TextArea>()
                .DerivesFrom<Control>()
                .IsConcreteClass()
                .IsUnsealed()
                .HasDefaultConstructor()
                .Implements<IAttributeAccesskey>()
                .Implements<IAttributeReadOnly>()
                .Implements<IXmlSerializable>()
                .XmlRoot("textarea")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            TextArea expected = new TextArea(new Name("id"))
            {
                Class = "class",
                Accesskey = 'a',
                Columns = 100,
                Disabled = true,
                Language = "fr",
                ReadOnly = true,
                Rows = 5,
                TabIndex = 1,
                Value = "Contains <span>text</span>"
            };

            string xml = "<textarea id='id' class='class' lang='fr' name='id' accesskey='a' cols='100' rows='5' tabindex='1' disabled='disabled' readonly='readonly'>Contains &lt;span&gt;text&lt;/span&gt;</textarea>";
            TextArea actual = xml.Deserialize<TextArea>();

            Assert.Equal<string>(expected.ToString(), actual.ToString());
        }

        [Fact]
        public void deserializeEmpty()
        {
            Assert.NotNull("<textarea />".Deserialize<TextArea>());
        }

        [Fact]
        public void serialize()
        {
            TextArea obj = new TextArea(new Name("id"))
            {
                Class = "class",
                Accesskey = 'a',
                Columns = 100,
                Disabled = true,
                Language = "fr",
                ReadOnly = true,
                Rows = 5,
                TabIndex = 1,
                Value = "Contains <span>text</span>"
            };

            string xpath = "1=count(/textarea[@id='id'][@class='class'][@lang='fr'][@name='id'][@tabindex='1'][@disabled='disabled'][@accesskey='a'][@cols='100'][@rows='5'][@readonly='readonly'][text()='Contains ']/span[text()='text'])";

            Assert.True((bool)obj.Serialize().CreateNavigator().Evaluate(xpath));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new TextArea());
        }

        [Fact]
        public void ctor_Name()
        {
            Assert.NotNull(new TextArea(new Name("id")));
        }

        [Fact]
        public void ctor_NameNull()
        {
            Assert.NotNull(new TextArea(null));
        }

        [Fact]
        public void prop_Accesskey()
        {
            Assert.True(new PropertyExpectations(typeof(TextArea).GetProperty("Accesskey"))
                .IsAutoProperty<char?>()
                .Result);
        }

        [Fact]
        public void prop_Columns()
        {
            Assert.True(new PropertyExpectations(typeof(TextArea).GetProperty("Columns"))
                .IsAutoProperty<int?>()
                .Result);
        }

        [Fact]
        public void prop_ReadOnly()
        {
            Assert.True(new PropertyExpectations(typeof(TextArea).GetProperty("ReadOnly"))
                .IsAutoProperty<bool>()
                .Result);
        }

        [Fact]
        public void prop_Rows()
        {
            Assert.True(new PropertyExpectations(typeof(TextArea).GetProperty("Rows"))
                .IsAutoProperty<int?>()
                .Result);
        }

        [Fact]
        public void prop_Value()
        {
            Assert.True(new PropertyExpectations(typeof(TextArea).GetProperty("Value"))
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void op_Deserialize_string_string_string()
        {
            TextArea expected = new TextArea(new Name("id"))
            {
                Class = "class",
                Accesskey = 'a',
                Columns = 100,
                Disabled = true,
                Language = "fr",
                ReadOnly = true,
                Rows = 5,
                TabIndex = 1,
                Value = "Contains <span>text</span>"
            };

            string xml = "<textarea id='id' class='class' lang='fr' name='id' accesskey='a' cols='100' rows='5' tabindex='1' disabled='disabled' readonly='readonly'>Contains &lt;span&gt;text&lt;/span&gt;</textarea>";
            TextArea actual = new TextArea().Deserialize("textarea", null, xml) as TextArea;

            Assert.Equal<string>(expected.ToString(), actual.ToString());
        }
    }
}